<?php

/**
 * 报名审核操作记录表
 * File Name：GoodsActionModel.php
 * File Encoding：UTF-8
 * File New Time：2014-5-12 11:14:29
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class GoodsActionModel extends BaseModel {

    public function tableName() {
        return '{{goods_action}}';
    }

    public function rules() {
        return array(
            array('product_id', 'required', 'message' => '商品ID不能为空！'),
            array('p_price', 'required', 'message' => '商品原价不能为空！'),
            array('note', 'required', 'message' => '操作理由不能为空！'),
            array('admin_id', 'required', 'message' => '该操作管理员ID不能为空！'),
            //无需验证字段
            array('promo_price,promo_price,uid,action_time,baoming_time,action_type,gid', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 审核状态标签
     * @param type $str 如果是字符串，获取对应键值，如果是数字，获取名称，为空，获取全部(数组)
     * @return array
     */
    public function getStatusTags($str = NULL) {
        $list = array(0 => '待审核', 1 => '审核通过', 2 => '审核不通过', 3 => '删除商品', 4 => '手动上传', 5 => '邮寄商品', 6 => '取消报名');
        if ($str === NULL) {
            return $list;
        } else {
            if (is_string($str)) {//传入字符，表示返回id
                return array_search($str, $list);
            } else {
                return isset($list[$str]) ? $list[$str] : false;
            }
        }
        return array();
    }

    /**
     * 操作记录处理
     * @param type $object 数据对象
     * @return boolean
     */
    public function goodsBmStatusActionLog($object) {
        if (!is_object($object) && !($object instanceof GoodsBmModel)) {
            $this->addError('', '对象错误，审核操作记录添加失败！');
            return false;
        }
        if ($object->status == 0) {
            return true;
        }
        //审核状态对应操作类型
        //$action_type 解释
        // 1 => 该商品审核通过成功！
        // 2 => 审核不通过更新成功！
        // 3 => 删除商品
        // 4 => 手动上传
        // 5 => 邮寄样品
        // 6 => 取消报名
        // 
        $action_type = array(1 => 1, 2 => 2, 3 => 5, 4 => 6);
        $this->product_id = $object->product_id;
        if ($object->status == 1) {
            $this->note = date('Y-m-d', $object->start_time) . ' 至 ' . date('Y-m-d', $object->end_time);
        } else {
            $this->note = $object->note;
        }
        if ($action_type[$object->status] != 6) {
            $this->admin_id = Yii::app()->passport->uid;
        } else {
            $this->admin_id = 0;
        }
        $this->action_time = time();
        $this->baoming_time = $object->add_time;
        $this->action_type = $action_type[$object->status];
        $this->p_price = $object->p_price;
        $this->promo_price = $object->promo_price;
        $this->product_img = $object->product_img;
        $this->uid = $object->uid;
        $this->gid = $object->primaryKey;
        //批量插入时莫名其妙的会写$this->id
        unset($this->id);
        //新增
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->save()) {
            return true;
        }
        $this->addError('', '审核操作记录添加失败！');
        return false;
    }

    /**
     * 后台商品管理，手动添加商品操作日志
     * @param GoodsModel $object 
     * @return boolean
     */
    public function goodsAddActionLog($object) {
        if (!is_object($object) && !($object instanceof GoodsModel)) {
            return false;
        }
        $this->product_id = $object->product_id;
        $this->note = '管理员后台添加';
        $this->admin_id = Yii::app()->passport->uid;
        $this->action_time = time();
        $this->baoming_time = $object->add_time;
        $this->action_type = 4; //手动上传
        $this->p_price = $object->p_price;
        $this->promo_price = $object->promo_price;
        $this->product_img = $object->product_img;
        $this->uid = 0;
        $this->gid = $object->primaryKey;
        //批量插入时莫名其妙的会写$this->id
        unset($this->id);
        //新增
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->save()) {
            return true;
        }
        return false;
    }

    /**
     * 商家报名时记录操作日志 action_type类型为0
     * @param GoodsBmModel $object
     * @return boolean
     */
    public function goodsBmAdd($object) {
        if (!is_object($object) && !($object instanceof GoodsBmModel)) {
            return false;
        }
        $this->product_id = $object->product_id;
        $this->note = '商家报名';
        $this->admin_id = 0;
        $this->action_time = time();
        $this->baoming_time = $object->add_time;
        $this->action_type = 0; //报名时增加的
        $this->p_price = $object->p_price;
        $this->promo_price = $object->promo_price;
        $this->product_img = $object->product_img;
        $this->uid = $object->uid;
        $this->gid = $object->primaryKey;
        //新增
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->save()) {
            return true;
        }
        return false;
    }

    //根据历史记录，去获取对应的报名记录
    public function getCommodity($object) {
        if (!is_object($object)) {
            return false;
        }
        $gid = $object->gid;
        if (!empty($gid)) {
            if ($object->action_type == 4) {
                $data = GoodsModel::model()->findByPk($gid);
            } else {
                $data = GoodsBmModel::model()->findByPk($gid);
            }
            if (empty($data)) {
                return false;
            }
            return $data->attributes;
        } else {
            return $this->getByProductId($object->product_id);
        }
    }

    /**
     * 根据商品ID获取对应信息
     * @param type $product_id 商品ID
     * @return type
     */
    public function getByProductId($product_id) {
        if (empty($product_id)) {
            return $product_id;
        }
        $key = "GoodsActionModel_getByProductId{$product_id}";
        //读取缓存
        $cache = Yii::app()->cache->get($key);
        if (!empty($cache)) {
            return $cache;
        }
        //先bm表
        $bm = GoodsBmModel::model()->find($this->where(array('product_id' => $product_id, 'select' => 'nick,start_time,end_time,product_id,goods_name,detail_url,product_img')));
        if (!empty($bm)) {
            $cache = $bm->attributes;
        } else {
            //再去goods表查询
            $goods = GoodsModel::model()->find($this->where(array('product_id' => $product_id, 'select' => 'nick,start_time,end_time,product_id,goods_name,detail_url,product_img')));
            if (!empty($goods)) {
                $cache = $goods->attributes;
            } else {
                //最后dede表查询
                $delete = GoodsDeleteModel::model()->find($this->where(array('product_id' => $product_id, 'select' => 'nick,start_time,end_time,product_id,goods_name,detail_url,product_img', 'order' => 'goods_id DESC')));
                if (!empty($delete)) {
                    $cache = $delete->attributes;
                }
            }
        }
        if (!empty($cache)) {
            Yii::app()->cache->set($key, $cache, 3600);
        } else {
            $cache = TaobaoApi::getInstance()->getItem($product_id);
        }
        return $cache;
    }

}
